import { specsList, specsCount } from '../../../request/api'
const state = {
    specslist: [],
    size: 2,
    page: 1,
    count: 0

}
const getters = {
    getSpecsList(state) {
        return state.specslist
    },
    
    getSpecsCount(state) {
        return state.count
    },
    getSize(state) {
        return state.size
    }
    ,
    getPage(state){
        return state.page
    }
}
const mutations = {
    REQ_SPECSLIST(state, payload) {
        state.specslist = payload;

    },
    REQ_GETCOUNT(state, payload) {
        state.count = payload;
    },
    CHANGE_PAGE(state, payload) {
        state.page = payload
    }
}
const actions = {
    getSpecsListActions(context) {
        specsList({
            size: context.state.size,
            page: context.state.page
        }).then(res => {
            if (res.code == 200) {
                context.commit('REQ_SPECSLIST', res.list)
                if (context.state.page != 1 && res.list.length == 0) {
                    context.dispatch('changePageAction', context.state.page - 1)
                }
            }
        })
    },
    getCountActions({commit}) {
        specsCount().then(res => {

            if (res.code == 200) {
                commit('REQ_GETCOUNT', res.list[0].total)
            }
        })
    },
    changePageAction(context, payload) {
        //第一、当用户切换页码，我们要修改page
        context.commit('CHANGE_PAGE', payload)
        //第二、重新调用列表
        context.dispatch('getSpecsListActions')
    }
}
export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}